import axios, { AxiosInstance } from "axios";
import { ElMessage } from "element-plus";

export const BASE_URL = import.meta.env.VITE_BASE_URL as string

export const BASE_WEBSOCKET = import.meta.env.VITE_SOCKET as string

const TIME_OUT = import.meta.env.XIOA_TIME_OUT as number

const service: AxiosInstance = axios.create({
    baseURL: BASE_URL,
    timeout: TIME_OUT,
    headers: {
        "Content-Type": "application/json;charset=utf-8",
    }
})

//请求拦截
service.interceptors.request.use((config) => {
    // config.headers = config.headers ?? null
    // if (pinia.token) {
    //     config.headers.token = pinia.token as string
    //     config.headers.Authorization = pinia.refreshToken as string || ""
    // }
    return config
})
//响应拦截
service.interceptors.response.use((res) => {
    const code: number = res.status
    if (code != 200) {
        return Promise.reject(res)
    }
    return res
}, (err) => {
    ElMessage(err)
})

export default service
